20
Easy2Siksha
The steps involved in DDA line generaon algorithm are:
▪ Input the two endpoints of the line segment, (x1,y1) and (x2,y2).
▪ Calculate the dierence between the x-coordinates and y-coordinates of the
endpoints as dx and dy respecvely.
▪ Calculate the slope of the line as m = dy/dx.
▪ Set the inial point of the line as (x1,y1).
▪ Loop through the x-coordinates of the line, incremenng by one each me, and
calculate the corresponding y-coordinate using the equaon y = y1 + m(x – x1).
▪ Plot the pixel at the calculated (x,y) coordinate.
▪ Repeat steps 5 and 6 unl the endpoint (x2,y2) is reached.
DDA algorithm is relavely easy to implement and is computaonally ecient, making it
suitable for real-me applicaons. However, it has some limitaons, such as the inability to
handle vercal lines and the need for oang-point arithmec, which can be slow on some
systems. Nonetheless, it remains a popular choice for generang lines in computer graphics.
In any 2-Dimensional plane, if we connect two points (x0, y0) and (x1, y1), we get a line
segment. But in the case of computer graphics, we can not directly join any two coordinate
points, for that, we should calculate intermediate points’ coordinates and put a pixel for
each intermediate point, of the desired color with the help of funcons like putpixel(x, y, K)
in C, where (x,y) is our co-ordinate and K denotes some color.
Examples:
Input: For line segment between (2, 2) and (6, 6) :
Output: we need (3, 3) (4, 4) and (5, 5) as our intermediate points.
Input: For line segment between (0, 2) and (0, 6) :
Output: we need (0, 3) (0, 4) and (0, 5) as our intermediate points.
For using graphics funcons, our system output screen is treated as a coordinate system
where the coordinate of the top-le corner is (0, 0) and as we move down our y-ordinate
increases, and as we move right our x-ordinate increases for any point (x, y). Now, for
generang any line segment we need intermediate points and for calculang them we can
use a basic algorithm called DDA(Digital dierenal analyzer) line generang algorithm.
DDA Algorithm:
Consider one point of the line as (X0, Y0) and the second point of the line as (X1, Y1).
// calculate dx , dy
dx = X1 – X0;
dy = Y1 – Y0;
// Depending upon absolute value of dx & dy
// choose number of steps to put pixel as